Page({
	data: {
			text: "This is page data.",
			ListOfL: [],
			selectedDepartment: '', // 当前选中的部门 ID
			selectedDepartmentName: '', // 当前选中的部门名称
			dataOfDocter: {
					id: '',
					d_pw: '',
					name: '',
					post: '',
					price: '',
					l_id: '',
			}
	},

	onLoad: function(options) {
			// 页面创建时执行
			wx.request({
					url: 'http://127.0.0.1:8080/api/u_leftList',
					method: 'GET',
					success: (res) => {
							if (res.data.status === 200) {
									this.setData({
											ListOfL: res.data.data
									});
									console.log(this.data.ListOfL);
							}
					},
					fail: (err) => {
							console.error('获取部门列表请求失败', err);
							wx.showToast({
									title: '请求失败',
									icon: 'none'
							});
					}
			});
	},

	onPriceInput: function(e) {
			this.setData({
					'dataOfDocter.price': e.detail.value
			});
	},

	onPostInput: function(e) {
			this.setData({
					'dataOfDocter.post': e.detail.value
			});
	},

	onNameInput: function(e) {
			this.setData({
					'dataOfDocter.name': e.detail.value
			});
	},

	onPwInput: function(e) {
			this.setData({
					'dataOfDocter.d_pw': e.detail.value
			});
	},

	onIdInput: function(e) {
			this.setData({
					'dataOfDocter.id': e.detail.value
			});
	},

	departmentChange: function(e) {
			console.log('radio发生change事件，携带value值为：', e.detail.value);
			const selectedId = e.detail.value;
			const selectedDepartment = this.data.ListOfL.find(item => item.l_id === selectedId);
			this.setData({
					'dataOfDocter.l_id': selectedId,
					selectedDepartment: selectedId,
					selectedDepartmentName: selectedDepartment ? selectedDepartment.office_name : ''
			});
	},

	registerDoctor: function() {
			const doctorData = this.data.dataOfDocter;
			if (!doctorData.id || !doctorData.d_pw || !doctorData.name || !doctorData.post || !doctorData.price || !doctorData.l_id) {
					wx.showToast({
							title: '请填写完整信息',
							icon: 'none'
					});
					return;
			}

			wx.request({
					url: 'http://127.0.0.1:8080/api/d_register', // 替换为实际的注册医生接口地址
					method: 'POST',
					data: doctorData,
					success: (res) => {
							if (res.data.status === 200) {
									wx.showToast({
											title: '注册成功',
											icon: 'success'
									});
									// 清空表单数据
									this.setData({
											dataOfDocter: {
													id: '',
													d_pw: '',
													name: '',
													post: '',
													price: '',
													l_id: '',
											},
											selectedDepartment: '',
											selectedDepartmentName: ''
									});
							} else {
									wx.showToast({
											title: '注册失败',
											icon: 'none'
									});
							}
					},
					fail: (err) => {
							console.error('注册医生请求失败', err);
							wx.showToast({
									title: '请求失败',
									icon: 'error'
							});
					}
			});
	},

	onShow: function() {
			// 页面出现在前台时执行
	},

	onReady: function() {
			// 页面首次渲染完毕时执行
	},

	onHide: function() {
			// 页面从前台变为后台时执行
	},

	onUnload: function() {
			// 页面销毁时执行
	},

	onPullDownRefresh: function() {
			// 触发下拉刷新时执行
	},

	onReachBottom: function() {
			// 页面触底时执行
	},

	onShareAppMessage: function() {
			// 页面被用户分享时执行
	},

	onPageScroll: function() {
			// 页面滚动时执行
	},

	onResize: function() {
			// 页面尺寸变化时执行
	},

	onTabItemTap(item) {
			// tab 点击时执行
			console.log(item.index);
			console.log(item.pagePath);
			console.log(item.text);
	},

	// 事件响应函数
	viewTap: function() {
			this.setData({
					text: 'Set some data for updating view.'
			}, function() {
					// this is setData callback
			});
	},

	// 自由数据
	customData: {
			hi: 'MINA'
	}
});